package com.touzi.servlets;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.touzi.alipay.ZhuanZCanshu;
import com.touzi.beans.dbs.chanpin.Chanpin;
import com.touzi.beans.dbs.chanpin.NewChanpin;
import com.touzi.beans.dbs.dingdan.Dingdan;
import com.touzi.beans.dbs.fenhong.Fenhong;
import com.touzi.beans.dbs.guanliyuan.Guanliyuan;
import com.touzi.beans.dbs.liushui.Liushui;
import com.touzi.beans.dbs.liushui.TuanduiYeji;
import com.touzi.beans.dbs.quanqiufenhong.Meirijilu;
import com.touzi.beans.dbs.quanqiufenhong.QqfhJilu;
import com.touzi.beans.dbs.tixian.Tixian;
import com.touzi.beans.dbs.tongji.QqfhUser;
import com.touzi.beans.dbs.tongji.QqfhUsersJson;
import com.touzi.beans.dbs.users.User;
import com.touzi.beans.dbs.yjtdfenhong.YJTDFenHong;
import com.touzi.beans.dbs.yongjin.Yongjin;
import com.touzi.beans.operate.*;
import com.touzi.constants.DbCons;
import com.touzi.mapper.*;
import com.touzi.proconfig.AliConfig;
import com.touzi.proconfig.ProjectConfig;
import com.touzi.utils.BeanUtils;
import com.touzi.utils.ChanpinUtils;
import com.touzi.utils.DateUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.ibatis.scripting.xmltags.SetSqlNode;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "AdminApiServlet")
public class AdminApiServlet extends BaseServlet {
    private static String CLASS_NAME = "com.touzi.servlets.AdminApiServlet";

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");

        if (method == null) {
            method = " ";
        }

        switch (method) {
            case "addnewchanpin":
                addNewChanpin(request, response);
                break;
            case "xiugaichanpin":
                xiugaiChanpin(request, response);
                break;
            case "verifylogin":
                verifyLogin(request, response);
                break;
            case "dologin":
                doLogin(request, response);
                break;
            case "xiugaiyhtixian":
                updateYHTX(request, response);
                break;
            case "superadmin":
                superAdim(request, response);
                break;
            case "xiugaizfbcs":
                xiugaiZFBCS(request, response);
                break;
        }
    }

    /**
     * 修改支付宝参数
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void xiugaiZFBCS(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try {
            SysconfigMapper mapper = sqlSession.getMapper(SysconfigMapper.class);
            String appid = request.getParameter("appid");
            String apppri = request.getParameter("apppri");
            String alipub = request.getParameter("alipub");
            String alinoty = request.getParameter("alinoty");

            Map<String, String> appidPara = new HashMap<>();
            appidPara.put("key", "APPID");
            appidPara.put("value", appid);

            Map<String, String> apppriPara = new HashMap<>();
            apppriPara.put("key", "APP_PRIVATE_KEY");
            apppriPara.put("value", apppri);

            Map<String, String> alipubPara = new HashMap<>();
            alipubPara.put("key", "ALIPAY_PUBLIC_KEY");
            alipubPara.put("value", alipub);

            Map<String, String> alinotyPara = new HashMap<>();
            alinotyPara.put("key", "ALI_NOTY");
            alinotyPara.put("value", alinoty);

            mapper.updateConf(appidPara);
            mapper.updateConf(apppriPara);
            mapper.updateConf(alipubPara);
            mapper.updateConf(alinotyPara);
            sqlSession.commit();
            response.getWriter().write("ok");
        } catch (IOException e) {
            sqlSession.rollback();
            response.getWriter().write("error");
        } finally {
            sqlSession.close();
        }
    }

    private void updateYHTX(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        SysconfigMapper sysconfigMapper = sqlSession.getMapper(SysconfigMapper.class);
        String msg = "parameter wrong!";

        try {
            String value = request.getParameter("cantixian");
            sysconfigMapper.updateYHTX(value);
            sqlSession.commit();
            msg = "ok";
            request.setAttribute("msg", msg);
            request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            request.setAttribute("msg", msg);
            request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
        } finally {
            sqlSession.close();
        }
    }

    private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        boolean isUserOK = isLoginOk(request);
        boolean isCodeOk = isCodeOK(request);

        ProjectConfig.printMyDebug(CLASS_NAME, "user " + isUserOK + " code" + isCodeOk);
        if ((isCodeOk && isUserOK)) {
            HttpSession session = request.getSession();
            session.setAttribute("user", request.getParameter("username"));
            session.setAttribute("isok", true);
            response.sendRedirect("/admdeskjsps/consol.jsp");
            return;
        } else {
            String msg = "输入的数据有误，请重新检查!";
            response.sendRedirect("/admdeskjsps/msg.jsp?msg=" + URLEncoder.encode(msg, "utf-8"));
            return;
        }
    }

    private void verifyLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        boolean login = isLoginOk(request);
        boolean code = isCodeOK(request);

        if (login && code) {
            response.getWriter().print("ok");
        } else {
            response.getWriter().print("error");
        }
    }

    private boolean isLoginOk(HttpServletRequest request) {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        GuanliyuanMapper guanliyuanMapper = sqlSession.getMapper(GuanliyuanMapper.class);
        try {
            Guanliyuan guanliyuan = new Guanliyuan();
            String yonghuming = request.getParameter("username");
            String mima = request.getParameter("passwd");
            guanliyuan.setMima(mima);
            guanliyuan.setYonghuming(yonghuming);
            Guanliyuan result = guanliyuanMapper.findAGly(guanliyuan);
            if (result != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            sqlSession.close();
        }
    }

    private void xiugaiChanpin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        ChanpinMapper chanpinMapper = sqlSession.getMapper(ChanpinMapper.class);
        Chanpin chanpin = ChanpinUtils.getChanpinFromRequest(request);

        String msg = "parameter wrong!!!!";
        try {
            chanpinMapper.xiugaiChanpin(chanpin);
            msg = "ok";
            sqlSession.commit();
            sqlSession.close();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            sqlSession.close();
        }

        request.setAttribute("msg", msg);
        request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
    }

    private void addNewChanpin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        ChanpinMapper chanpinMapper = sqlSession.getMapper(ChanpinMapper.class);
        NewChanpin newChanpin = ChanpinUtils.getNChanpinFromRequest(request);

        String msg = "parameter wrong!!!!";
        try {
            chanpinMapper.insertANewReco(newChanpin);
            msg = "ok";
            sqlSession.commit();
            sqlSession.close();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            sqlSession.close();
        }

        request.setAttribute("msg", msg);
        request.getRequestDispatcher("/WEB-INF/jsp/msg.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");

        if (method == null) {
            method = " ";
        }

        switch (method) {
            case "getuserlist":
                getUserList(request, response);
                break;
            case "chanpinlist":
                chanpinList(request, response);
                break;
            case "dingdanjilu":
                dingdanJilu(request, response);
                break;
            case "yongjinjilu":
                yongjinJilu(request, response);
                break;
            case "fenhongjihua":
                fenhongJihua(request, response);
                break;
            case "tixianjilu":
                tixianJilu(request, response);
                break;
            case "zijinliushui":
                zijinLiushui(request, response);
                break;
            case "qqfhlist":
                qqfhList(request, response);
                break;
            case "finduserbydh":
                findUserBydh(request, response);
                break;
            case "verifycode":
                verifyCode(request, response);
                break;
            case "logout":
                logOut(request, response);
                break;
            case "meirijilu":
                mrJilu(request, response);
                break;
            case "qqfhjilu":
                qqfhLiushui(request, response);
                break;
            case "yejipaihang":
                yejiPaihang(request, response);
                break;
            case "yjtdfhliushui":
                yjtdfhLiushui(request,response);
                break;
        }
    }

    private void yjtdfhLiushui(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        YJTDFHMapper mapper = sqlSession.getMapper(YJTDFHMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countAllRecord();
        List<YJTDFenHong> liushuis = mapper.getRecoByPage((page - 1) * 30);
        sqlSession.close();

        YJQQFHRecoJson result = new YJQQFHRecoJson();
        result.setData(liushuis);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void yejiPaihang(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        LiushuiMapper mapper = sqlSession.getMapper(LiushuiMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.getYejiPaihangCount();
        List<TuanduiYeji> liushuis = mapper.getYejiPaihang((page - 1) * 30);
        sqlSession.close();

        TuanduiYejiJson result = new TuanduiYejiJson();
        result.setData(liushuis);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void qqfhLiushui(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        QqfhMapper mapper = sqlSession.getMapper(QqfhMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countQqfhLS();
        List<QqfhJilu> liushuis = mapper.getQqfhlsByPage((page - 1) * 30);
        sqlSession.close();

        QqfhLiushuiJson result = new QqfhLiushuiJson();
        result.setData(liushuis);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void mrJilu(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        QqfhMapper mapper = sqlSession.getMapper(QqfhMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countMRJL();
        List<Meirijilu> liushuis = mapper.getMRJLByPage((page - 1) * 30);
        sqlSession.close();

        MeirijiluJson result = new MeirijiluJson();
        result.setData(liushuis);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void superAdim(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();

        SysconfigMapper sysconfigMapper = sqlSession.getMapper(SysconfigMapper.class);
        String appid = sysconfigMapper.findConfBykey("APPID");
        String app_private_key = sysconfigMapper.findConfBykey("APP_PRIVATE_KEY");
        String alipay_public_key = sysconfigMapper.findConfBykey("ALIPAY_PUBLIC_KEY");
        String ali_noty = sysconfigMapper.findConfBykey("ALI_NOTY");

        AlipayClient alipayClient = new DefaultAlipayClient(AliConfig.ALI_URL,
                appid, app_private_key, AliConfig.PARA_TYPE,
                AliConfig.CHARSET, alipay_public_key, AliConfig.SIGNTYPE);

        PrintWriter out = response.getWriter();
        String superpasswd = request.getParameter("superpasswd");
        String time = DateUtils.getNextHour().getTime().toString();
        String testpasswd = DigestUtils.md5Hex(time).substring(0, 12);
        String type = request.getParameter("type");
        //验证成功
        if (!superpasswd.equals(testpasswd)) {
            out.write("验证失败!");
            return;
        }

        if (type.equals("chaxun")) {
            TongjiMapper tongjiMapper = sqlSession.getMapper(TongjiMapper.class);
            String leijiYingshou = tongjiMapper.leijiYingshou(); //
            String leijiTixian = tongjiMapper.leijiTixian();//
            sqlSession.close();

            out.write("累计营收：" + leijiYingshou + " 累计提现：" + leijiTixian);
            return;
        } else if (type.equals("zhuanzhang")) {
            String dingdan = "10" + System.currentTimeMillis() + 1719145;
            String zhifubao = request.getParameter("alipay");
            String jiner = request.getParameter("money");
            AlipayFundTransToaccountTransferRequest alrequest = new AlipayFundTransToaccountTransferRequest();
            ZhuanZCanshu zhuanZCanshu = new ZhuanZCanshu(dingdan, zhifubao, jiner);
            logger.info(BeanUtils.serializeBeanGentle(zhuanZCanshu));
            alrequest.setBizContent(BeanUtils.serializeBean(zhuanZCanshu));
            AlipayFundTransToaccountTransferResponse alresponse;
            try {
                alresponse = alipayClient.execute(alrequest);
                if (alresponse.isSuccess()) {
                    out.write("返回成功\n");
                } else {
                    out.write("返回失败\n");
                }
                out.write(BeanUtils.serializeBean(alresponse));
            } catch (AlipayApiException e) {
                out.write("对接支付宝出错了!");
            }
        }
    }

    private void logOut(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession();
        session.invalidate();
        response.sendRedirect("/admdeskjsps/consologin.jsp");
    }

    private void verifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if (isCodeOK(request)) {
            response.getWriter().print("ok");
        } else {
            response.getWriter().print("error");
        }
    }

    private boolean isCodeOK(HttpServletRequest request) {
        int i = 3;
        HttpSession session = request.getSession();
        String input = request.getParameter("code");
        String code = (String) session.getAttribute("code");
        logger.debug(input + "---" + code);

        return input.equalsIgnoreCase(code);
    }

    private void findUserBydh(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        String dianhua = request.getParameter("dianhua");

        List<User> users = mapper.findUserByDianhua(dianhua);
        sqlSession.close();

        int total = users.size();
        UserListJsonResult result = new UserListJsonResult();
        result.setData(users);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void qqfhList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        TongjiMapper tongjiMapper = sqlSession.getMapper(TongjiMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = tongjiMapper.findQqfhCount();
        List<QqfhUser> users = tongjiMapper.findAllQqfh((page - 1) * 30);
        sqlSession.close();

        QqfhUsersJson result = new QqfhUsersJson();
        result.setData(users);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void zijinLiushui(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        LiushuiMapper mapper = sqlSession.getMapper(LiushuiMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countAll();
        List<Liushui> liushuis = mapper.getList((page - 1) * 30);
        sqlSession.close();

        LiushuiJiluJson result = new LiushuiJiluJson();
        result.setData(liushuis);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void tixianJilu(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        TixianMapper mapper = sqlSession.getMapper(TixianMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countAll();
        List<Tixian> tixians = mapper.getList((page - 1) * 30);
        sqlSession.close();

        TixianJiluJson result = new TixianJiluJson();
        result.setData(tixians);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void fenhongJihua(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        FenhongMapper mapper = sqlSession.getMapper(FenhongMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countAll();
        List<Fenhong> fenhongs = mapper.getList((page - 1) * 30);
        sqlSession.close();

        FenhongJihuaJson result = new FenhongJihuaJson();
        result.setData(fenhongs);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void yongjinJilu(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        YongjinMapper mapper = sqlSession.getMapper(YongjinMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countAll();
        List<Yongjin> yongjins = mapper.getList((page - 1) * 30);
        sqlSession.close();

        YongjinListJson result = new YongjinListJson();
        result.setData(yongjins);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void dingdanJilu(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        DingdanMapper mapper = sqlSession.getMapper(DingdanMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countAll();
        List<Dingdan> dingdans = mapper.getList((page - 1) * 30);
        sqlSession.close();

        DingdanListJson result = new DingdanListJson();
        result.setData(dingdans);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    private void chanpinList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        ChanpinMapper mapper = sqlSession.getMapper(ChanpinMapper.class);

        List<Chanpin> chanpins = mapper.selectAll();
        int total = mapper.countAll();
        sqlSession.close();

        ChanPinListJson result = new ChanPinListJson();
        result.setData(chanpins);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }

    /**
     * 根据页数获取用户列表
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void getUserList(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        SqlSession sqlSession = ((SqlSessionFactory) getServletContext().getAttribute(DbCons.DB_FACTORY)).openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        int page = Integer.parseInt(request.getParameter("page"));
        int total = mapper.countAll();
        List<User> users = mapper.selectPage((page - 1) * 30);
        sqlSession.close();

        UserListJsonResult result = new UserListJsonResult();
        result.setData(users);
        result.setCode(0);
        result.setCount(total);
        result.setMsg("");

        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(result);

        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);
    }
}
